{% extends '_base_list.html' %}

{% block title %}
    代码发布
{% endblock %}


{% block top_css %}
    {{ super() }}
    <!--bees-->
    <link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='css/bees.css') }}">

{% endblock %}


<!-- Content head -->
{% block head_title %}
    代码发布
{% endblock %}
{% block head_nav %}
    <ol class="breadcrumb">
        <li><a href="#"><i class="fa fa-home"></i></a></li>
        <li><a href="#">代码发布</a></li>
        <li class="active">项目管理</li>
    </ol>
{% endblock %}


<!-- Content body -->
{% block title_filter %}
    <div class="filter">
        <div class="row">
            <div class="col-xs-12 col-sm-2">
                <div class="form-group">
                    <input type="text" class="form-control" name="create_time" placeholder="请选择创建时间" autocomplete="off">
                </div>
            </div>
            <div class="col-xs-12 col-sm-3 pull-right">
                <div class="form-group">
                    <div class="input-group">
                        <input type="text" name="search" placeholder="请输入项目名称" class="form-control">
                        <span class="input-group-btn">
                            <button name="search" class="btn btn-mint" type="button">
                                <i class="fa fa-search"></i>
                            </button>
                        </span>
                    </div>
                </div>
            </div>
        </div>
    </div>
{% endblock %}

{% block body_operator %}
    <div class="operator mar-btm">
        <div class="row">
            <div class="col-xs-12 text-center">
                {% if current_user.has_perm('codes-project-create') %}
                    <button class="btn btn-primary btn-sm" id="add"><i class=""></i> 新增项目</button>
                {% endif %}
                {% if current_user.has_perm('codes-project-update') %}
                    <button class="btn btn-mint btn-sm" id="edit"><i class=""></i> 修改项目</button>
                {% endif %}
                {% if current_user.has_perm('codes-project-delete') %}
                    <button class="btn btn-danger btn-sm" id="del"><i class=""></i> 删除项目</button>
                {% endif %}
                {% if current_user.has_perm('codes-project-permission') %}
                    <button class="btn btn-purple btn-sm" id="perm"><i class=""></i> 项目授权</button>
                {% endif %}
            </div>
        </div>
    </div>
{% endblock %}

{% block body_table %}
   <table id="projectTable" class="table table-auto">
   </table>
{% endblock %}


{% block buttom_js %}
    {{ super() }}
    <!--bees-->
    <script src="{{ url_for('static', filename='js/bees.js') }}"></script>

    <script type="text/javascript">
        $(function () {
            bees.initDaterangepicker($('input[name="create_time"]'),{
                opens: "right",
                timePicker: false,
                timePickerSeconds: false,
                singleDatePicker: false,
                locale: {
                    format: 'YYYY-MM-DD'
                }
            });

            // 回填和初始化默认过滤参数
            var createTimeEle = $("input[name='create_time']");
            var searchEle = $('input[name="search"]');

            var filterParams = JSON.parse(window.sessionStorage.getItem("filterParams"));
            if(filterParams){
                // 回填表单过滤条件
                createTimeEle.val(filterParams.create_time);
                searchEle.val(filterParams.search);
                window.sessionStorage.removeItem("filterParams");
            }else {
                // 默认表单过滤条件
                filterParams = {
                    'create_time': createTimeEle.val(),
                    'search': searchEle.val()
                };
            }
            $.fn.beesTable.defaultFilterParams(filterParams);

            // 初始化表格
            var $tableEle = $("#projectTable");
            var options = {
                columns: [
                    {title: '', field: 'selected', radio: true},
                    {title: '名称', field: 'name', sortable: false},
                    {title: '环境', field: 'environment', sortable: false},
                    {title: '仓库类型', field: 'repo_type', sortable: false,
                        formatter: function (value, row, index, field) {
                            // value: 字段值; row: 行记录数据; index: 行索引 ;field: 行字段
                            if(value === 'git'){
                                return '<i class="fa fa-lg fa-git text text-primary text-bold" aria-hidden="true"></i>'
                            }
                            else if(value === 'svn'){
                                return '<span class="sub-parent">' +
                                            '<i class="fa fa-scribd text text-primary" aria-hidden="true"></i>' +
                                            '<span class="sub-first">v</span><span class="sub-second">n</span>' +
                                       '</span>'
                            }
                        }
                    },
                    {title: '上线方式', field: 'repo_mode', sortable: false},
                    {title: '状态', field: 'status', sortable: false,
                        formatter: function (value, row, index, field) {
                            // value: 字段值; row: 行记录数据; index: 行索引 ;field: 行字段
                            if(value === 1){
                                return '<span class="text text-success text-bold">启用</span>'
                            }
                            else if(value === 0){
                                return '<span class="text text-danger text-bold">禁用</span>'
                            }
                        }
                    },
                    {title: '分组', field: 'label'},
                    {title: '管理者', field: 'permission'},
                    {title: '创建人', field: 'create_by'},
                    {title: '创建时间', field: 'create_time', sortable: true}
                ],
                url:'{{ url_for("codes.api-project-list") }}',
                sortName: 'create_time',
                sortOrder: 'desc'
            };
            var table = $tableEle.beesTable(options);

            // 搜索
            bees.bindTableSearch({
                table: $tableEle,
                searches: [
                    {eventType:'change', eventSource:createTimeEle, data:function () {
                            return {'create_time': createTimeEle.val()};
                        }
                    },
                    {eventType:'click', eventSource:$('button[name="search"]'), data: function () {
                            return {'search': searchEle.val()}
                        }
                    }
                ]
            });

            // 操作
            bees.bindTableOperator({
                table: $tableEle,
                operator: {
                    add: {
                        href: '{{ url_for('codes.project-create') }}'
                    },
                    show: {
                        disable: true
                    },
                    edit: {
                        href: '{{ url_for('codes.project-update', pk=default_pk) }}'
                    },
                    del: {
                        confirmCallback: function (data) {
                            setAjaxCSRFToken();
                            $.ajax({
                                type: 'post',
                                url: '{{ url_for("codes.project-delete", pk=default_pk) }}'.replace(bees.defaultPK, data.id),
                                data : {},
                                success: function (result) {
                                    if (result.code != 200) {
                                        bees.tipError({
                                            message: result.data.message
                                        });
                                    }else {
                                       bees.tipSuccess({
                                            message: "删除成功"
                                        });
                                    }

                                    //reload
                                    table.bt.bootstrapTable('refresh');
                                }
                            })
                        }
                    },
                    perm: {
                        href: '{{ url_for('codes.project-permission', pk=default_pk) }}',
                        ele: $('#perm'),
                        msg: "请选择需要授权的行"
                    }
                }
            });
        })
    </script>
{% endblock %}
